<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="生产号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['productionTaskListOdd']" placeholder="请输入生产号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item v-if="title=='完工'" label="货品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-dict-select-search type="list" v-decorator="['finishedGoodsName', validatorRules.finishedGoodsName]" :trigger-change="true" dictCode="bas_material,name,id" @sendNameId="getNameId" />
                    </a-form-item>
                    <a-form-item v-else label="货品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" style="display:none;">
                        <j-dict-select-search type="list" v-decorator="['finishedGoodsName']" :trigger-change="true" dictCode="bas_material,name,id" @sendNameId="getNameId" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item v-show="title=='完工'" label="货品编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.finishedGoodsCode" placeholder="请输入货品编号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择日期" v-decorator="['date']" :trigger-change="true" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.unit" placeholder="请输入单位"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item v-if="title=='编辑'" label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly  v-decorator="['specifications']" placeholder="请输入规格"></a-input>
                    </a-form-item>
                    <a-form-item v-else label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.finishedSpecifications" placeholder="请输入规格"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="开工日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择开工日期" v-decorator="['startsDate']" :trigger-change="true" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col v-if="title=='完工'" :span="12">
                    <a-form-item label="完工日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <j-date placeholder="请选择完工日期" v-decorator="['completionDate']" :trigger-change="true" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="实际生产数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input-number v-decorator="['productionQuantity']" placeholder="请输入实际生产数量" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item v-if="title=='编辑' && state != '完工'" label="被派人员" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-select v-decorator="['sentPersonnel', validatorRules.sentPersonnel]" placeholder="请输入被派人员">
                            <a-select-option 
                                v-for="deptUser in deptUserList" :key="deptUser.id" 
                                :value="deptUser.realname" @click="sentPersonnelId = deptUser.id"
                            >
                                {{deptUser.realname}}
                            </a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item v-else label="被派人员" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['sentPersonnel']" placeholder="请输入被派人员"></a-input>
                    </a-form-item>
                </a-col>
                <a-col v-if="title=='完工'" :span="12">
                    <a-form-item label="完工数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['completeNumber', validatorRules.completeNumber]" placeholder="请输入完工数量"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['note']" placeholder="请输入备注"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction,
    getGoodsInfo
} from '@/api/manage'
import {
    getDeptUserList
} from '@/api/api'
import pick from 'lodash.pick'
import JDate from '@/components/jeecg/JDate'

export default {
    name: "WorksheetModal",
    components: {
        JDate,
    },
    data() {
        return {
            form: this.$form.createForm(this),
            title: "操作",
            width: 800,
            visible: false,
            model: {},
            dataList: {
                unit: '',
                worksheetType: '造粒'
            },
            labelCol: {
                sm: {
                    span: 6
                },
            },
            wrapperCol: {
                sm: {
                    span: 16
                },
            },
            confirmLoading: false,
            validatorRules: {
                finishedGoodsName: {
                    rules: [{
                        required: true,
                        message: "请选择货品名称!"
                    }]
                },
                sentPersonnel: {
                    rules: [{
                        required: true,
                        message: "请选择派遣人员!"
                    }]
                },
                completeNumber: {
                    rules: [{
                        required: true,
                        message: "请输入完工数量!"
                    }]
                },
            },
            url: {
                add: "/production/worksheet/add",
                edit: "/production/worksheet/edit",
                complete: "/production/worksheet/complete"
            },
            deptUserList: [],
            sentPersonnelId: '',
            state: '',
        }
    },
    created(){
        this.getDeptUserList()
    },
    methods: {
        //选择货品后请求货品数据并进行响应式赋值
        async getNameId(value) {
            this.dataList.finishedGoodsId = value
            let result = await getGoodsInfo({
                params: {
                    id: value,
                },
            })
            let {
                dataList
            } = this;
            let {
                code,
                model,
            } = result.result
            this.$set(dataList, 'finishedGoodsCode', code)
            this.$set(dataList, 'finishedSpecifications', model)
        },
        async getDeptUserList(){
            let res = await getDeptUserList({
                id: '390ea49f37f34bb8ae74b5a972ad1f0f',
            })
            this.deptUserList = res.result
        },
        add() {
            this.edit({});
        },
        edit(record) {
            this.form.resetFields();
            this.dataList = {
                unit: '公斤',
                worksheetType: '造粒',
            }
            this.model = Object.assign({}, record);
            this.visible = true;
            this.state = record.state
            this.sentPersonnelId = record.sentPersonnelId
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'finishedGoodsName', 'productionTaskListOdd', 'odd', 'date', 'startsDate', 'completionDate', 'productionQuantity', 'sentPersonnel', 'completeNumber', 'note', 'specifications'))
            })
        },
        close() {
            this.$emit('close');
            this.visible = false;
        },
        handleOk() {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (this.state == '完工' && this.title == '编辑') {
                    this.$message.warning('此任务已派工!');image.png
                    return false;
                }
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        if (this.title == '完工') {
                            httpurl += this.url.complete;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                    }
                    Object.assign(values, this.dataList)
                    let formData = Object.assign(this.model, values);
                    formData.sentPersonnelId = this.sentPersonnelId
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        that.close();
                    })
                }

            })
        },
        handleCancel() {
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'finishedGoodsName', 'productionTaskListOdd', 'odd', 'date', 'startsDate', 'completionDate', 'productionQuantity', 'sentPersonnel', 'completeNumber', 'note', 'specifications'))
        },
    }
}
</script>
